package com.dw.etl.core.springbatch.reader;

import com.dw.etl.core.api.MdMedTbService;
import com.dw.etl.core.bean.model.MdMedTb;
import com.dw.etl.core.db.DynamicDataSource;
import com.dw.etl.core.db.ThreadLocalDataSource;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.NonTransientResourceException;
import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;
import org.springframework.batch.item.database.JdbcCursorItemReader;
import org.springframework.batch.item.database.JdbcPagingItemReader;
import org.springframework.batch.item.database.builder.JdbcCursorItemReaderBuilder;
import org.springframework.batch.item.database.support.SqlPagingQueryProviderFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.ColumnMapRowMapper;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.util.List;
import java.util.Map;

/**
 * Created by liujunlei on 2021/5/26.
 */
@Component
public class SimpleReader {


    @Bean(name = "simpleReaderBean")
    @StepScope
    public JdbcCursorItemReader<Map<String,Object>> simpleReaderBean(@Value("#{jobParameters[qSql]}") String qSql, @Value("#{jobParameters[idDbSou]}") String idDbSou) {
        return new JdbcCursorItemReaderBuilder<Map<String,Object>>()
                .dataSource((DataSource) DynamicDataSource.dataSourceMap.get(idDbSou))
                .name("simpleReaderBean")
                .sql(qSql)
                .rowMapper(new ColumnMapRowMapper())
                .build();
    }

    @Bean(name = "mdMedTbReaderBean")
    @StepScope
    public JdbcCursorItemReader<Map<String,Object>>  mdMedTbReaderBean(@Value("#{jobParameters[idDbSou]}") String idDbSou) {
        String sql = "select * from md_med_tb where FG_DEL=0";
        return new JdbcCursorItemReaderBuilder<Map<String,Object>>()
                .dataSource((DataSource) DynamicDataSource.dataSourceMap.get(idDbSou))
                .name("mdMedTbReaderBean")
                .sql(sql)
                .rowMapper(new ColumnMapRowMapper())
                .build();

    }
}
